﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharp屏幕录制
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 声明一个 Rectangle 对象，用来指定矩形的位置和大小
        /// </summary>
        private Rectangle _bounds;

        /// <summary>
        /// 获取鼠标位置的 Rectangle 对象值
        /// </summary>
        /// <returns></returns>
        private static Rectangle GetMousePositionRectangle()
        {
            return new Rectangle(MousePosition.X + -5, MousePosition.Y + -5, 32, 32);
        }

        /// <summary>
        /// 获取 桌面屏幕图片
        /// </summary>
        /// <returns></returns>
        private Bitmap GetScreenImgByteArray()
        {
            Bitmap bitmap = new Bitmap(_bounds.Width, _bounds.Height, PixelFormat.Format24bppRgb);

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(Point.Empty, Point.Empty, _bounds.Size, CopyPixelOperation.SourceCopy);
                GC.Collect();
                return bitmap;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            DoubleBuffered = true;
            _bounds = pictureBox.Bounds;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (pictureBox.Image != null)
            {
                pictureBox.Image.Dispose();
            }
            pictureBox.Image = GetScreenImgByteArray();
        }
    }
}
